<?php
class worklog extends Controller
{
	function __constructor()
	{
		parent::__constructor();
	}
	
	//--
	function index()
	{
		$this->load->view("worklog/index");
	}
	
	function newworklog()
	{
		is_logged_in();
		
		$msg = "";
		$this->load->model("model_user");
		$this->load->model("model_job");
		$this->load->model("model_company");
		
		$techs = $this->model_user->getUsers(999999, array("user.lastname", "asc"),  array("active" => 1));
		$jobs = $this->model_job->getJobs(999999,array("job.created", "desc", array("job.job_status" => 1)));
		$clients = $this->model_company->getAll();
		
		if($this->input->post("action") == "new")
		{
			$jobId = $this->input->post("jobId");
			$techId = $this->input->post("techId");
			
			if($jobId != "" && $techId != "")
			{
				$this->load->model("model_worklog");
				$this->model_worklog->newWorklog($jobId, $techId);
				$msg = "Work Log created successfully";
			}
		}
		
		$data = array( 	"crum" => build_crum( array("Admin", "Work Logs", "New Work Log") )
						,"techs" => $techs
						,"jobs" => $jobs
						,"clients" => $clients
						,"msg" => $msg
					);
		$this->load->view("worklog/newworklog", $data);
	}
	
	function showallworklogs()
	{
		is_logged_in();
		
		$msg= "";
		$this->load->model("model_worklog");
		$this->load->model("model_lu");
		
		$status = $this->model_lu->get("job_status");
		
		$worklogs = $this->model_worklog->getWorklogs(99999999);
		
		$data = array(	"crum" => build_crum( array("Work Logs", "Show All Work Logs") )
							  ,"wls" => $worklogs
							  ,"status" => $status
							  ,"msg" => $msg);
							  
		$this->load->view("worklog/showworklogs",$data);
	}
	
	
	function editworklog()
	{
		is_logged_in();
		
		$logId = $this->uri->segment(3);

		if($logId != "")
		{
			$msg = "";
			$this->load->model("model_user");
			$this->load->model("model_job");
			$this->load->model("model_company");
			$this->load->model("model_worklog");
			
			$techs = $this->model_user->getUsers(999999, array("user.lastname", "asc"),  array("active" => 1));
			$jobs = $this->model_job->getJobs(999999,array("job.created", "desc", array("job.job_status" => 1)));
			$clients = $this->model_company->getAll();
			$worklog = $this->model_worklog->getWorklogs(1,array("wl.created", "desc"), array("wl.id" => $logId));
			
			
			$company_id = $worklog[0]['company_id'];
			$wl_jobs = $this->model_job->getJobs(99999, array("job.created", "desc"), $where = array("company_id" => $company_id));
			
			$jobs_array = "";
			
			foreach($wl_jobs as $wl_job)
			{
				$jobs_array .= "<option value='".$wl_job['id']."'";
				if($wl_job['id'] == $worklog[0]['job_id'] )
					$jobs_array .= " selected ";
				$jobs_array .= ">#" . $wl_job['id'] ." - " .word_limiter($wl_job['job_subject'],5) . "</option>";
			}
		
			
			$data = array( 	"crum" => build_crum( array("Admin", "Work Logs", "New Work Log") )
							,"techs" => $techs
							,"jobs" => $jobs
							,"clients" => $clients
							,"msg" => $msg
							,"wl" => $worklog[0]
							,"job_options" => $jobs_array
						);
			$this->load->view("worklog/editworklog", $data);
		}
		
	}
	
	function updateworklog()
	{
			if($this->input->post("wlId") != "")
			{
				$this->load->model("model_worklog");
				
				$jobId = $this->input->post("jobId");
				$techId = $this->input->post("techId");
				$wl_id = $this->input->post("wlId");
				
				if($jobId != "" && $techId != "")
				{
					$update_data = array("tech_id" => $techId
										 ,"job_id" => $jobId);
					
					$this->model_worklog->updateWorkLog($update_data, $wl_id);
					$msg = "Work Log update successfully";
					redirect(base_url()."worklog/showallworklogs");
					
				}
				return;
			}
	}
}